// pages/discover/index.js
import images from '../../utils/images.js'
const app = getApp()
Page({
  data: {
    ...app.globalData.config,
    tabList: [
      {name: '最新', key: 'new'},
      {name: '全部', key: 'all'}
    ],

    typeArray: [
      {id:1, name:"税"},
      {id:2, name:"票"},
      {id:3, name:"跨境电商贷"},
      {id:4, name:"个人经营性贷款"}
    ],
    productTypeValue: '',
    productTypeText: '',

    buidDateValue: '',

    borrowerIdentityArray: [{id: 1, name:"企业法人"}, {id: 2, name: "企业股东"}],
    borrowerIdentityValue: '',
    borrowerIdentityText: '',

    provinceCityGroup: [],
    multiArray: [],
    selectedProvinceCity: {proviceId: '', cityId: '', cityName: '请选择'},
    images,
    legalPersonChangedMonth_hidden: true,

    lastTaxLevelArray: ["A", "B", "C", "D", "M"],
    lastTaxLevelValue: ''
  },
  handleChange({ detail }){
    this.setData({
      current: detail.key
    })
  },

  bindTypeChange(e) {
    this.setData({
      productTypeValue: this.data.typeArray[e.detail.value].id,
      productTypeText: this.data.typeArray[e.detail.value].name
    });
  },

  bindBuildDateChange(e) {
    this.setData({
      buidDateValue: e.detail.value
    });
  },

  bindBorrowerIdentityChange(e) {
    this.setData({
      borrowerIdentityValue: this.data.borrowerIdentityArray[e.detail.value].id,
      borrowerIdentityText: this.data.borrowerIdentityArray[e.detail.value].name,
    });
  },

  bindMultiPickerData() {
    var that = this, 
        provinceList = [], 
        cityList = [],
        index = 0;
    
    wx.request({
      url: app.globalData.context + '/setting/dict/multi/region/2',
      success: function(res) {
        var result = res.data;
        if (result.code == 'success' && result.data != null && result.data.length > 0) {
          that.setData({
            provinceCityGroup: result.data[0].children
          });

          that.data.provinceCityGroup.forEach(ele => {
            provinceList.push(ele.title);
            if (index == 0) {
                ele.children.forEach(e => {
                  cityList.push(e.title);
                });
            }
            index++;
          });
      
          var mArray = [];
          mArray.push(provinceList);
          mArray.push(cityList);
          that.setData({
            multiArray:mArray
          });
        }
      }
    });
    
    
  },
  bindMultiPickerChange(e) {
    var that = this,
        selectedProvinceCity = {};
    var province = that.data.provinceCityGroup[e.detail.value[0]];
    var city = province.children[e.detail.value[1]];
    selectedProvinceCity.proviceId = province.id;
    selectedProvinceCity.cityId = city.id;
    selectedProvinceCity.cityName = city.title;

    this.setData({
      selectedProvinceCity: selectedProvinceCity
    });
  },
  bindMultiPickerColumnChange(e) {
      var that = this;
      switch (e.detail.column) {
        case 0:
          var province = that.data.provinceCityGroup[e.detail.value];
          var cityArray = province.children;
          var list = []
          for (var i = 0; i < cityArray.length; i++) {
            list.push(cityArray[i].title)
          }

          var mArray = that.data.multiArray, 
            multiIndex = [];
          mArray[1] = list
          multiIndex[0] = e.detail.value;
          multiIndex[1] = 0;
          that.setData({
            multiArray: mArray,
            multiIndex: multiIndex
          });
      }
  },

  matchProduct(e) {
    var info = e.detail.value;

    if (info.companyName == '') {
      wx.showToast({
        title: '请输入企业名称',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    if (info.companyDate == '') {
      wx.showToast({
        title: '请选择企业成立时间',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    if (info.borrowerIdentity == '') {
      wx.showToast({
        title: '请选择借款人身份',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    
    var navigateUrl = '/pagesProduct/discoverResult/index?';
    for (var key in info) {
        info[key] = encodeURIComponent(info[key]);
        navigateUrl += key + '=' + info[key] + '&';
    }
    navigateUrl = navigateUrl.substring(0, navigateUrl.length - 1);
    wx.navigateTo({
        url: navigateUrl
    });
  },

  legalPersonChanged0(e) {
    this.setData({
      legalPersonChangedMonth_hidden: true
    });
  },
  legalPersonChanged1(e) {
    this.setData({
      legalPersonChangedMonth_hidden: false
    });
  },

  lastTaxLevelChange(e) {
      this.setData({
        lastTaxLevelValue: this.data.lastTaxLevelArray[e.detail.value]
      });
  },

  onLoad(){
    setTimeout(()=>{
      this.selectComponent("#page").hideLoading()
    },2000)

    this.bindMultiPickerData();
  },
  onShareAppMessage() {
    return {
      title: '企运融'
    }
  },
  onShareTimeline() {}
})